//
//  CityTourCardVC.m
//  ImGuider
//
//  Created by 王鹏宇 on 2017/6/9.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "CityTourCardVC.h"
#import "MRLineLayout.h"
#import "CityTourCardCell.h"
#import "LinePreDetailVC.h"
@interface CityTourCardVC ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UICollectionView * collectionView;
@property (nonatomic, strong) NSMutableArray * dataSource;
@end

@implementation CityTourCardVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupBg];
    [self createCollection];
    
    [self loadDataSource];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [MobClick beginLogPageView:@"2"];
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
    [MobClick endLogPageView:@"2"];
}
- (void)dealloc {
    
    [MobClick event:@"2_1_1"];
}

- (void)setupBg {
    
    [[[self.navigationController.navigationBar subviews] objectAtIndex:0] setAlpha:0];
    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    _imageView.image = self.Bgimage;
    _imageView.backgroundColor = kThemeColor;
    [self.view addSubview:_imageView];
}

- (void)loadDataSource {
    [ActivityIndicatorView showHUDAddedTo:self.view];
    [NetAPIManager getAllLinesBy:self.cityID start:@(0) count:@(0) block:^(NSArray *linesArray, NSError *error) {
       [ActivityIndicatorView hideHUDAddedTo:self.view];
        self.dataSource = [NSMutableArray arrayWithArray:linesArray];
        [self.collectionView reloadData];
    }];
}

- (void)createCollection {
    
    //创建Collection
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) collectionViewLayout:[self setLaout]];
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor clearColor];
    _collectionView.delegate = self;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    [_collectionView registerNib:[UINib nibWithNibName:@"CityTourCardCell" bundle:nil] forCellWithReuseIdentifier:@"CityTourCardCell"];
    [self.view addSubview:_collectionView];
    
    // [self setRefresh];
    
}


- (UICollectionViewFlowLayout *)setLaout {
    //创建自定义布局
    MRLineLayout * layout = [[MRLineLayout alloc] init];
    
    // 设置UICollectionView中每个Item的size
    CGFloat width =  SCREEN_WIDTH - 100;
    CGFloat space = (ISiPhone5 || ISiPhone4) ?  135:145;
    layout.itemSize = CGSizeMake(width, width +space);
    
    return layout;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CityTourCardCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CityTourCardCell" forIndexPath:indexPath];
    
    LineModel * model = self.dataSource[indexPath.row];
    
    [cell configDataWith:model];
    
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    [collectionView deselectItemAtIndexPath:indexPath animated:NO];
    
    LineModel * model = self.dataSource[indexPath.row];
    
    LinePreDetailVC *detailVC = [IGUtils loadViewController:@"LinePreDetailVC" storyboard:@"LineDetail"];
    detailVC.lineid = model.lineid;
    
    [self.navigationController pushViewController:detailVC animated:YES];
    
    [MobClick event:@"2_1_2"];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
